package cn.anecansaitin.inet.mixins;

import cn.anecansaitin.inet.TranslateHelper;
import mcjty.lib.container.GenericContainer;
import mcjty.lib.gui.GenericGuiContainer;
import mcjty.lib.gui.ManualEntry;
import mcjty.lib.gui.Window;
import mcjty.lib.gui.widgets.Panel;
import mcjty.lib.gui.widgets.TextField;
import mcjty.lib.gui.widgets.ToggleButton;
import mcjty.lib.typed.TypedMap;
import mcjty.lib.varia.OrientationTools;
import mcjty.xnet.modules.cables.blocks.ConnectorTileEntity;
import mcjty.xnet.modules.cables.client.GuiConnector;
import mcjty.xnet.setup.XNetMessages;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.player.Inventory;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import static mcjty.lib.gui.widgets.Widgets.*;
import static mcjty.xnet.modules.cables.blocks.ConnectorTileEntity.*;
import static mcjty.xnet.modules.cables.client.GuiConnector.HEIGHT;
import static mcjty.xnet.modules.cables.client.GuiConnector.WIDTH;

@Mixin(GuiConnector.class)
public abstract class CablesGuiMixin extends GenericGuiContainer<ConnectorTileEntity, GenericContainer> {
    @Final
    @Shadow
    private ToggleButton[] toggleButtons;

    public CablesGuiMixin(ConnectorTileEntity tileEntity, GenericContainer container, Inventory inventory, ManualEntry manualEntry) {
        super(tileEntity, container, inventory, manualEntry);
    }

    @Override
    public void init() {
        super.init();

        Panel toplevel = vertical().filledRectThickness(2);

        TextField nameField = new TextField().name("name").tooltips(TranslateHelper.getText("inet.cables.text_field.name.tooltip"));

        Panel namePanel = horizontal().children(label(TranslateHelper.getText("inet.cables.label.name.text")), nameField);
        toplevel.children(namePanel);

        Panel togglePanel = horizontal().
                children(label(TranslateHelper.getText("inet.cables.label.directions.name")));
        for (Direction facing : OrientationTools.DIRECTION_VALUES) {
            toggleButtons[facing.ordinal()] = new ToggleButton().text(facing.getSerializedName().substring(0, 1).toUpperCase())
                    .event(() -> {
                        sendServerCommandTyped(XNetMessages.INSTANCE, CMD_ENABLE,
                                TypedMap.builder()
                                        .put(PARAM_FACING, facing.ordinal())
                                        .put(PARAM_ENABLED, toggleButtons[facing.ordinal()].isPressed())
                                        .build());
                    });
            toggleButtons[facing.ordinal()].pressed(tileEntity.isEnabled(facing));
            togglePanel.children(toggleButtons[facing.ordinal()]);
        }
        toplevel.children(togglePanel);

        toplevel.bounds(leftPos, topPos, WIDTH, HEIGHT);
        window = new Window(this, toplevel);

        window.bind(XNetMessages.INSTANCE, "name", tileEntity, "name");
    }
}
